package org.testis.testdriver.translation;

import org.testis.testdriver.TestDataObject;
import org.testis.testdriver.TestIterator;
import org.testis.testdriver.TestPart;
import org.testis.testdriver.TestStep;
import java.util.LinkedList;

/**
 *
 * @author barisergun
 */
public class AtofTestDataTranslateAction extends AtofValueTranslateAction {

    private TestPart m_testPartInRun;
    public AtofTestDataTranslateAction(TestPart curTestPart)
    {
        m_testPartInRun = curTestPart;
    }
    @Override
    public String operateRequest(String dataPointer)
    {
        int beginIndex = dataPointer.indexOf(AtofTranslationConstants.TESTDATA_CONSTANT);
        while (beginIndex != -1) {
            int keyBeginIndex = beginIndex + 9;
            int keyEndIndex = dataPointer.indexOf("@",
                                                  keyBeginIndex);
            int doBeginIndex = keyEndIndex + 3;
            int doEndIndex = dataPointer.indexOf("@",
                                                 doBeginIndex);
            int tsBeginIndex = doEndIndex + 3;
            int tsEndIndex;
            tsEndIndex = dataPointer.indexOf("#",
                                             tsBeginIndex);

            if (dataPointer.indexOf("#",
                                    tsBeginIndex) != -1) {
                tsEndIndex = dataPointer.indexOf("#",
                                                 tsBeginIndex) + 1;
            }
            else if (dataPointer.indexOf(" ",
                                         tsBeginIndex) != -1) {
                tsEndIndex = dataPointer.indexOf(" ",
                                                 tsBeginIndex);
            }
            else if (dataPointer.indexOf('\0',
                                         tsBeginIndex) != -1) {
                tsEndIndex = dataPointer.indexOf('\0',
                                                 tsBeginIndex);
            }
            else {
                tsEndIndex = dataPointer.length();
            }

            String keyPointer = dataPointer.substring(
                keyBeginIndex,
                keyEndIndex);
            String doPointer = dataPointer.substring(
                doBeginIndex,
                doEndIndex);
            String tsPointer = dataPointer.substring(
                tsBeginIndex,
                tsEndIndex);
            tsPointer = tsPointer.replace("#",
                                          "");
            String toBeReplaced = dataPointer.substring(beginIndex,
                                                        tsEndIndex);
            String replacing = findPreviousResult(keyPointer,
                                                  doPointer,
                                                  tsPointer);
            
            if (replacing == null) {
                throw new UnidentifiedTranslationException(
                    "No value found for TestData Key: " + keyPointer );
            }
            
            dataPointer = dataPointer.replace(toBeReplaced,
                                              replacing);

            beginIndex = dataPointer.indexOf(AtofTranslationConstants.TESTDATA_CONSTANT,
                                             tsEndIndex);
        }
        return dataPointer;
    }
    
    private String findPreviousResult(String key,
                                      String dataObjId,
                                      String tsId)
    {
        TestPart targetTestStep = null;
        TestPart parentTestCase = m_testPartInRun.getParent();
        TestIterator innerIter = parentTestCase.createIter();
        innerIter.init();
        while (innerIter.isDone()) {

            innerIter.next();
            TestPart curStepCont = innerIter.currentItem();
            if (curStepCont.getPartId().equals(tsId)) {
                targetTestStep = curStepCont;
                break;
            }
        }
        if (targetTestStep == null) {
            return "null";
        }
        TestDataObject curDataObj = null;
        TestStep toCheck = (TestStep) targetTestStep;
        LinkedList<TestDataObject> testDatas = toCheck.getTestDataList();
        for (int y = 0; y < testDatas.size(); y++) {
            TestDataObject dataObj = testDatas.get(y);
            if (dataObj.getDataObjectId().equals(dataObjId)) {
                curDataObj = dataObj;
                break;
            }
        }
        if (curDataObj == null) {
            return "null";
        }
        if (curDataObj.getResultMap() == null) {
            return "null";
        }


        String valueToGet = curDataObj.getResultMap().get(key);
        if (valueToGet == null) {
            return "null";
        }
        return valueToGet;
    }


}
